package cn.iutils.mailbox.controller;

import cn.iutils.common.Page;
import cn.iutils.common.controller.BaseController;
import cn.iutils.common.utils.JStringUtils;
import cn.iutils.mailbox.entity.MailSend;
import cn.iutils.mailbox.service.MailSendService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
* 发件箱 控制器
* @author iutils.cn
* @version 1.0
*/
@Controller
@RequestMapping("${adminPath}/mailbox/mailSend")
public class MailSendController extends BaseController {

    @Autowired
    private MailSendService mailSendService;

    @ModelAttribute
    public MailSend get(@RequestParam(required = false) String id) {
        MailSend entity = null;
        if (JStringUtils.isNotBlank(id)) {
            entity = mailSendService.get(id);
        }
        if (entity == null) {
            entity = new MailSend();
        }
        return entity;
    }

    @RequiresPermissions("mailbox:mailSend:view")
    @RequestMapping()
    public String list(Model model, Page<MailSend> page) {
        model.addAttribute("page", page.setList(mailSendService.findPage(page)));
        return "mailbox/mailSend/list";
    }

    @RequiresPermissions("mailbox:mailSend:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(MailSend mailSend,Model model) {
        model.addAttribute("mailSend", mailSend);
        return "mailbox/mailSend/form";
    }

    @RequiresPermissions("mailbox:mailSend:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String create(MailSend mailSend, RedirectAttributes redirectAttributes) {
        mailSendService.save(mailSend);
        addMessage(redirectAttributes,"新增成功");
        return "redirect:"+ adminPath +"/mailbox/mailSend/update?id="+mailSend.getId();
    }

    @RequiresPermissions("mailbox:mailSend:update")
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(MailSend mailSend, Model model) {
        model.addAttribute("mailSend", mailSend);
        return "mailbox/mailSend/form";
    }

    @RequiresPermissions("mailbox:mailSend:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(MailSend mailSend, RedirectAttributes redirectAttributes) {
        mailSendService.save(mailSend);
        addMessage(redirectAttributes,"修改成功");
        return "redirect:"+ adminPath +"/mailbox/mailSend/update?id="+mailSend.getId();
    }

    @RequiresPermissions("mailbox:mailSend:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public String delete(@PathVariable("id") String id,int pageNo,int pageSize, RedirectAttributes redirectAttributes) {
        mailSendService.delete(id);
        addMessage(redirectAttributes,"删除成功");
        return "redirect:"+ adminPath +"/mailbox/mailSend?pageNo="+pageNo+"&pageSize="+pageSize;
    }
}
